
package GUI;

import java.awt.*;
import java.util.EnumMap;

import javax.swing.*;

import map.Map;
import map.Resolution;
import map.Texture;

import controller.ImageLoader;
import controller.LocalManager;
import controller.ImageLoader.CornerDirection;
import controller.ImageLoader.Direction;



public class MapPanel extends JPanel{
    
	public MapPanel(){
		this(Resolution.MID);
    }
	
	public MapPanel(Resolution startResolution){
		this.map = LocalManager.getMap();
		currentRes = startResolution;
		mapPanels = new EnumMap<Resolution, JPanel>(Resolution.class);
		refreshMap();
    	this.setOpaque(false);
    }
	
	public MapPanel(Map map, Resolution startResolution){
		this.map = map;
		currentRes = startResolution;
		mapPanels = new EnumMap<Resolution, JPanel>(Resolution.class);
		refreshMap();
    	this.setOpaque(false);
    }
    
    public JPanel createMapPanel(Resolution res){
    	JPanel globalMap = new JPanel();
    	globalMap.setOpaque(false);
    	layeredPane = new JLayeredPane();
    	layeredPane.setPreferredSize(new Dimension(map.getWidth()*res.getFactor(), map.getHeight()*res.getFactor()));
    	layeredPane.setOpaque(false);
    	int n, index;
    	JLabel l;
    	Dimension size;
    	for(int i = 0; i<map.getWidth(); i++)
    		for(int j = 0; j<map.getHeight(); j++){
    			Texture t = map.getTexture(i, j);
    			if(t != null){
    				int gridX = i*res.getFactor();
    				int gridY = j*res.getFactor();
    				if(t == Texture.CITY){
    					l = new JLabel(ImageLoader.getCityImage(res, 0));
    					size = l.getPreferredSize();
    					l.setBounds(gridX, gridY-res.getFactor()/2, size.width, size.height);
    					layeredPane.add(l, JLayeredPane.MODAL_LAYER);
    					//add land tile under
    					t = Texture.LAND;
    				}
    				n = ImageLoader.getIndexToDivide(t);
        			index = (j % n) * n + (i % n);
        			ImageIcon tile = ImageLoader.getTile(t, res, index).getImageIcon();
    				l = new JLabel(tile);
    				size = l.getPreferredSize();
    				l.setBounds(gridX, gridY, size.width, size.height);
    				layeredPane.add(l, JLayeredPane.DEFAULT_LAYER);
    				
    				
    				Texture rightDown, right, down;
    				right = map.getTexture(i+1, j); //add border
    				down = map.getTexture(i, j+1); //add border
    				rightDown = map.getTexture(i+1, j+1); //add corn
    				
    				if(right != null && right != t && (right== Texture.WATER || t== Texture.WATER)){
    					int borderGridX = (i+1)*res.getFactor() - res.getBordFactor();
    					int borderGridY = j*res.getFactor();
    					
    					ImageIcon icon1;
    					if(t==Texture.WATER)
    						icon1 = ImageLoader.getBeachEdge(res, Direction.WEST);
    					else
    						icon1 = ImageLoader.getBeachEdge(res, Direction.EAST);
						JLabel border = new JLabel(icon1);
						Dimension sizeB = border.getPreferredSize();
						border.setBounds(borderGridX, borderGridY, sizeB.width, sizeB.height);
						layeredPane.add(border, JLayeredPane.PALETTE_LAYER);
						
    				}
    				if(down != null && down != t && (down == Texture.WATER || t== Texture.WATER)){
    					int borderGridX = i*res.getFactor();
    					int borderGridY = (j+1)*res.getFactor() - res.getBordFactor();
    					
    					ImageIcon icon1;
    					if(t==Texture.WATER)
    						icon1 = ImageLoader.getBeachEdge(res, Direction.NORTH);
    					else
    						icon1 = ImageLoader.getBeachEdge(res, Direction.SOUTH);
						JLabel border = new JLabel(icon1);
						Dimension sizeB = border.getPreferredSize();
						border.setBounds(borderGridX, borderGridY, sizeB.width, sizeB.height);
						layeredPane.add(border, JLayeredPane.PALETTE_LAYER);
						
    				}
    				if(rightDown != null){
    					ImageIcon icon1 = null;
    					int water = map.getBoxEqualTextures(i, j, 2, 2, Texture.WATER);
    					if(water == 3){
    						if(t != Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.NORTH_WEST);
    						else if(right != Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.NORTH_EAST);
    						else if(down != Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.SOUTH_WEST);
    						else if(rightDown != Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.SOUTH_EAST);
    					}else if(water == 1){
    						if(t == Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.NORTH_WEST);
    						else if(right == Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.NORTH_EAST);
    						else if(down == Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.SOUTH_WEST);
    						else if(rightDown == Texture.WATER)
    							icon1 = ImageLoader.getBeachCorner(res, CornerDirection.SOUTH_EAST);
    					}
	    				if(icon1 != null){
	    					int borderGridX = (i+1)*res.getFactor() - res.getBordFactor();
	    					int borderGridY = (j+1)*res.getFactor() - res.getBordFactor();
		    				JLabel border = new JLabel(icon1);
							Dimension sizeB = border.getPreferredSize();
							border.setBounds(borderGridX, borderGridY, sizeB.width, sizeB.height);
							layeredPane.add(border, JLayeredPane.PALETTE_LAYER);
	    				}
    				}
    			}
    		}
    	globalMap.add(layeredPane);
    	return globalMap;
    }
    
//	try {
//  BufferedImage img1 = ImageIO.read(new File("immagine1.jpg"));
//  BufferedImage img2 = ImageIO.read(new File("immagine2.png"));
//
//  Graphics2D g = img1.createGraphics();
//
//  g.drawImage(img2, 100, 100, null);
//  g.dispose();
//
//  ImageIO.write(img1, "jpg", new File("output.jpg"));
//} catch (Exception e) {
//  System.out.println(e);
//}
    
    public void setResolution(Resolution res){
    	if(res != currentRes){
    		currentRes = res;
    		this.removeAll();
    		if(mapPanels.get(currentRes) == null){
				mapPanels.put(currentRes, createMapPanel(currentRes));
    		}
			this.add(mapPanels.get(currentRes));
    	}
    }
    
    public void refreshMap(){
    	this.removeAll();
    	Resolution[] resolutions = Resolution.values();
    	for(int i = 0; i<resolutions.length; i++)
    		mapPanels.put(resolutions[i], null);
    	mapPanels.put(currentRes, this.createMapPanel(currentRes));
    	this.add(mapPanels.get(currentRes));
    	this.revalidate();
    }
    
  //to delete
    public void smoothMap(){
    	map.smooth();
    	refreshMap();
    }

    

//	img = img.getScaledInstance(600, 200 Image.SCALE_FAST);
//
//    objBI = new BufferedImage(200, 600,  BufferedImage.TYPE_INT_RGB);
//
//    g2 = objBI.createGraphics();
//
//    g2.rotate(Math.toRadians(90), 600/2 , 200/2);
//
//    g2.drawImage(img, -200, 200, null);
//	
//    
//	g2.rotate(Math.toRadians(90));
//	g2.drawImage(img, 0, -200, null);
    
    private Resolution currentRes;
    private EnumMap<Resolution, JPanel> mapPanels;
    private Map map;
    private JLayeredPane layeredPane;
	private static final long serialVersionUID = 1L;
}
